import { defineStore } from 'pinia'
const template = {
  "id": "end_1",
  "type": "end",
  "data": {
    "title": "结束节点",
    "mode": "2",
    "content": "",
    "input": [{
      "ref": "",
      "name": "result",
      "category": "1",
      "value": "",
    }]
  }
}

export const useEndStore = defineStore({
  id: 'useEndStore',
  state: () => ({
    template
  }),
  actions: {
    init(item:any){
      if(item){
      this.template.id = item.id
      this.template.type = item.type
      this.template.data.mode = item.data.mode
      this.template.data.title = item.data.title
      this.template.data.content = item.data.content
      this.template.data.input =item.data.input&& [...item.data.input.map(el=>{
        return {
          ...el,
          "Options":[{
            label: el.value,
            value: el.value
          }]
        }
      })]
    }
    },
    add() {
        this.template.data.input.push(
          {
            "ref": "",
            "name": "",
            "category": "1",
            "value": "",
            "Options":[{
              label:'',
              value:''
            }]
          }
        )
    },
    updateProps(attrName:string,val:string){
      this.template.data[attrName] = val;
    },
    deleteItem(index:number) {
      this.template.data.input.splice(index, 1);
    },
    updateRef(id:string){
      this.template.data.input.forEach(item=>{
        item.ref = id
      })
    },
    clearData(){
      this.template.data.input = this.template.data.input.map(item=>{
        item.value = ''
        return item
      })
    }
  }
})
